package service

import (
	"gowork/hotter/model"
	"gowork/hotter/variable"
	"strconv"
	"strings"
)

type HotterService struct {
}

func NewHotterService() *HotterService {
	return &HotterService{}
}

func (hs *HotterService) HotRenewal(versionNumber string, platform string) map[string]interface{} {

	var hcis []*model.HeatChangeInformation
	variable.Db.Where("platform = ?", platform).Order("created_at desc").Find(&hcis)

	paths := strings.Split(hcis[0].FileName, "\\")
	newest_numbers := strings.Split(strings.Split(paths[len(paths)-1], "_")[1], ".")
	numbers := strings.Split(versionNumber, ".")

	for i := 0; i < len(numbers); i++ {
		number_int, _ := strconv.Atoi(numbers[i])
		newest_number_int, _ := strconv.Atoi(newest_numbers[i])
		if number_int < newest_number_int {
			return map[string]interface{}{
				"code":    200,
				"message": "成功",
				"data": map[string]string{
					"fileName": hcis[0].FileName,
					"paths":    paths[len(paths)-1],
				},
			}
		}
	}

	return map[string]interface{}{"code": 200, "message": "已是最新版本", "data": nil}
}
